package com.packet.controller;

import com.packet.utils.AliOSSUtils;
import com.packet.pojo.Customs;
import com.packet.pojo.Trip;
import com.packet.service.StaffCustomsService;
import com.packet.service.StaffTripService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;

/**
 * @ClassName:StaffCustomController
 * @Author: mu
 * @Date:2023-12-07 16:48
 * @Describe:
 */
@Controller
public class StaffCustomController {
    @Autowired
    private StaffCustomsService staffCustomsService;
    @Autowired
    private StaffTripService staffTripService;
    @Autowired
    private AliOSSUtils aliOSSUtils;

    @RequestMapping("/staff/addAdvence")
    public String addCustomsAdvence(Customs customs, HttpSession session,Integer addContinue){

        Integer sid = (Integer)session.getAttribute("sid");
        Integer newTripBid = staffTripService.getNewTripBid(sid);
        customs.setBid(newTripBid);
        Integer newCustomsCid = staffCustomsService.getNewCustomsCid(newTripBid);
        if(newCustomsCid == null){
            customs.setCId(1);
        }else{
            customs.setCId(newCustomsCid+1);
        }
        customs.setCadvence("0");

        System.out.println(customs);
        staffCustomsService.addCustoms(customs);

        System.out.println(addContinue);
        if(addContinue == 0 ){
            return "redirect:/staff/doingList";
        }else {
            return "/staff/customs/addAdvence";
        }

    }

    @RequestMapping("/staff/exchange/{bid}")
    public String exchange(@PathVariable("bid") Integer bid, Model model){
        model.addAttribute("paymentBid",bid);
        return "/staff/customs/addCustoms";
    }

    @PostMapping ("/staff/addCustoms/{bid}")
    public String addCustoms(Customs customs, @PathVariable("bid") Integer bid,MultipartFile image,Integer addContinue) throws Exception {
        String upload = aliOSSUtils.upload(image);
        customs.setInvoice(upload);

        customs.setCadvence("0");

        Integer newCustomsCid = staffCustomsService.getNewCustomsCid(bid);
        if(newCustomsCid == null){
            customs.setCId(1);
        }else{
            customs.setCId(newCustomsCid+1);
        }

        staffCustomsService.addCustoms(customs);

        if(addContinue == 0 ){
            Trip trip = staffTripService.getDetailTrip(bid);
            trip.setState("4");
            staffTripService.updateTrip(trip);
            return "redirect:/staff/doingList";
        }else {
            return "/staff/customs/addCustoms";
        }
    }

}
